<?php
/**
 * Date: 2018/2/7
 * Time: 13:48
 */

namespace app\common\library;

class Openssl
{
    protected $key = '8NONassdfgssewr35JtHesysWpM';
    public  function encrypt($string)
    {

        // openssl_encrypt 加密不同Mcrypt，对秘钥长度要求，超出16加密结果不变
        $data = openssl_encrypt($string, 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
        $data = strtolower(bin2hex($data));
        return $data;
    }


    /**
     * @param string $string 需要解密的字符串
     * @param string $key 密钥
     * @return string
     */
    public  function decrypt($string)
    {
        $decrypted = openssl_decrypt(hex2bin($string), 'AES-128-ECB', $this->key, OPENSSL_RAW_DATA);
        return $decrypted;
    }

}